<template>
  <div class="mecanum-wheel-container">
    <div class="header">
      <button class="back-btn" @click="goBack">
        <el-icon><ArrowLeft /></el-icon>
      </button>
      <h1 class="title">麦克纳姆轮控制</h1>
      <div class="connection-status">
        <span :class="['status-indicator', carStore.statusClass]"></span>
        <span class="status-text">{{ carStore.statusText }}</span>
      </div>
    </div>

    <div class="content landscape-container">
      <div class="landscape-left">
        <!-- 轮子控制区域 -->
        <div class="control-section">
          <h3 class="section-title">四轮独立控制</h3>
          <div class="wheel-controls">
            <div class="wheel-row">
              <div class="wheel-item">
                <label class="wheel-label">左前轮 (L1)</label>
                <div class="slider-container">
                  <input 
                    type="range" 
                    v-model="wheelSpeeds.l1" 
                    min="-100" 
                    max="100" 
                    class="speed-slider"
                    :disabled="!carStore.isConnected"
                    @input="updateWheelSpeed"
                  />
                  <span class="speed-value">{{ wheelSpeeds.l1 }}</span>
                </div>
              </div>
              
              <div class="wheel-item">
                <label class="wheel-label">右前轮 (R1)</label>
                <div class="slider-container">
                  <input 
                    type="range" 
                    v-model="wheelSpeeds.r1" 
                    min="-100" 
                    max="100" 
                    class="speed-slider"
                    :disabled="!carStore.isConnected"
                    @input="updateWheelSpeed"
                  />
                  <span class="speed-value">{{ wheelSpeeds.r1 }}</span>
                </div>
              </div>
            </div>
            
            <div class="wheel-row">
              <div class="wheel-item">
                <label class="wheel-label">左后轮 (L2)</label>
                <div class="slider-container">
                  <input 
                    type="range" 
                    v-model="wheelSpeeds.l2" 
                    min="-100" 
                    max="100" 
                    class="speed-slider"
                    :disabled="!carStore.isConnected"
                    @input="updateWheelSpeed"
                  />
                  <span class="speed-value">{{ wheelSpeeds.l2 }}</span>
                </div>
              </div>
              
              <div class="wheel-item">
                <label class="wheel-label">右后轮 (R2)</label>
                <div class="slider-container">
                  <input 
                    type="range" 
                    v-model="wheelSpeeds.r2" 
                    min="-100" 
                    max="100" 
                    class="speed-slider"
                    :disabled="!carStore.isConnected"
                    @input="updateWheelSpeed"
                  />
                  <span class="speed-value">{{ wheelSpeeds.r2 }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 预设动作区域 -->
        <div class="control-section">
          <h3 class="section-title">预设动作</h3>
          <div class="preset-buttons">
            <button 
              class="btn-secondary preset-btn" 
              @click="setPreset('forward')"
              :disabled="!carStore.isConnected"
            >
              <el-icon><Top /></el-icon>
              前进
            </button>
            
            <button 
              class="btn-secondary preset-btn" 
              @click="setPreset('backward')"
              :disabled="!carStore.isConnected"
            >
              <el-icon><Bottom /></el-icon>
              后退
            </button>
            
            <button 
              class="btn-secondary preset-btn" 
              @click="setPreset('left')"
              :disabled="!carStore.isConnected"
            >
              <el-icon><Back /></el-icon>
              左移
            </button>
            
            <button 
              class="btn-secondary preset-btn" 
              @click="setPreset('right')"
              :disabled="!carStore.isConnected"
            >
              <el-icon><Right /></el-icon>
              右移
            </button>
            
            <button 
              class="btn-secondary preset-btn" 
              @click="setPreset('rotateLeft')"
              :disabled="!carStore.isConnected"
            >
              <el-icon><RefreshLeft /></el-icon>
              左转
            </button>
            
            <button 
              class="btn-secondary preset-btn" 
              @click="setPreset('rotateRight')"
              :disabled="!carStore.isConnected"
            >
              <el-icon><RefreshRight /></el-icon>
              右转
            </button>
            
            <button 
              class="btn-secondary preset-btn" 
              @click="setPreset('stop')"
              :disabled="!carStore.isConnected"
            >
              <el-icon><VideoPause /></el-icon>
              停止
            </button>
            
            <button 
              class="btn-secondary preset-btn" 
              @click="setPreset('diagonal')"
              :disabled="!carStore.isConnected"
            >
              <el-icon><Position /></el-icon>
              斜行
            </button>
          </div>
        </div>
      </div>

      <div class="landscape-right">
        <!-- 轮子状态显示 -->
        <div class="control-section">
          <h3 class="section-title">轮子状态</h3>
          <div class="wheel-status">
            <div class="wheel-visualization">
              <div class="car-body">
                <div class="wheel wheel-lf" :class="{ active: wheelSpeeds.l1 !== 0 }">
                  <span class="wheel-speed">{{ wheelSpeeds.l1 }}</span>
                </div>
                <div class="wheel wheel-rf" :class="{ active: wheelSpeeds.r1 !== 0 }">
                  <span class="wheel-speed">{{ wheelSpeeds.r1 }}</span>
                </div>
                <div class="wheel wheel-lr" :class="{ active: wheelSpeeds.l2 !== 0 }">
                  <span class="wheel-speed">{{ wheelSpeeds.l2 }}</span>
                </div>
                <div class="wheel wheel-rr" :class="{ active: wheelSpeeds.r2 !== 0 }">
                  <span class="wheel-speed">{{ wheelSpeeds.r2 }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 控制信息 -->
        <div class="control-section">
          <h3 class="section-title">控制信息</h3>
          <div class="control-info">
            <div class="info-item">
              <span class="info-label">连接状态:</span>
              <span :class="['info-value', carStore.statusClass]">
                {{ carStore.statusText }}
              </span>
            </div>
            <div class="info-item">
              <span class="info-label">左前轮:</span>
              <span class="info-value">{{ wheelSpeeds.l1 }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">右前轮:</span>
              <span class="info-value">{{ wheelSpeeds.r1 }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">左后轮:</span>
              <span class="info-value">{{ wheelSpeeds.l2 }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">右后轮:</span>
              <span class="info-value">{{ wheelSpeeds.r2 }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useCarStore } from '../stores/car'
import { commonApi } from '../utils/api'
import { 
  ArrowLeft, 
  Top, 
  Bottom, 
  Back, 
  Right, 
  RefreshLeft, 
  RefreshRight, 
  VideoPause,
  Position
} from '@element-plus/icons-vue'

const router = useRouter()
const carStore = useCarStore()

// 轮子速度
const wheelSpeeds = reactive({
  l1: 0,
  l2: 0,
  r1: 0,
  r2: 0
})

// 页面加载时获取连接状态
onMounted(async () => {
  await carStore.getConnectionStatus()
  // 初始化轮子速度为当前状态
  wheelSpeeds.l1 = carStore.wheelSpeeds.l1
  wheelSpeeds.l2 = carStore.wheelSpeeds.l2
  wheelSpeeds.r1 = carStore.wheelSpeeds.r1
  wheelSpeeds.r2 = carStore.wheelSpeeds.r2
})

// 返回上一页
const goBack = () => {
  router.back()
}

// 更新轮子速度
const updateWheelSpeed = async () => {
  if (!carStore.isConnected) return
  
  const result = await carStore.wheelControl(
    wheelSpeeds.l1,
    wheelSpeeds.l2,
    wheelSpeeds.r1,
    wheelSpeeds.r2
  )
  
  if (!result.success) {
    commonApi.showError(result.message)
  }
}

// 设置预设动作
const setPreset = async (preset) => {
  if (!carStore.isConnected) return
  
  let speeds = { l1: 0, l2: 0, r1: 0, r2: 0 }
  
  switch (preset) {
    case 'forward':
      speeds = { l1: 50, l2: 50, r1: 50, r2: 50 }
      break
    case 'backward':
      speeds = { l1: -50, l2: -50, r1: -50, r2: -50 }
      break
    case 'left':
      speeds = { l1: -50, l2: 50, r1: 50, r2: -50 }
      break
    case 'right':
      speeds = { l1: 50, l2: -50, r1: -50, r2: 50 }
      break
    case 'rotateLeft':
      speeds = { l1: -50, l2: -50, r1: 50, r2: 50 }
      break
    case 'rotateRight':
      speeds = { l1: 50, l2: 50, r1: -50, r2: -50 }
      break
    case 'stop':
      speeds = { l1: 0, l2: 0, r1: 0, r2: 0 }
      break
    case 'diagonal':
      speeds = { l1: 50, l2: 0, r1: 0, r2: 50 }
      break
  }
  
  // 更新本地状态
  wheelSpeeds.l1 = speeds.l1
  wheelSpeeds.l2 = speeds.l2
  wheelSpeeds.r1 = speeds.r1
  wheelSpeeds.r2 = speeds.r2
  
  // 发送控制指令
  const result = await carStore.wheelControl(
    wheelSpeeds.l1,
    wheelSpeeds.l2,
    wheelSpeeds.r1,
    wheelSpeeds.r2
  )
  
  if (result.success) {
    commonApi.showSuccess('预设动作执行成功')
  } else {
    commonApi.showError(result.message)
  }
}
</script>

<style scoped>
.mecanum-wheel-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 20px;
  background: linear-gradient(135deg, #f5f3ff 0%, #e9d5ff 100%);
}

.header {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  gap: 16px;
}

.back-btn {
  background: rgba(255, 255, 255, 0.8);
  border: none;
  border-radius: 12px;
  padding: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.9);
  transform: translateY(-2px);
}

.title {
  flex: 1;
  font-size: 24px;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
}

.connection-status {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 20px;
  backdrop-filter: blur(10px);
}

.status-text {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.control-section {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 24px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: var(--shadow-light);
}

.section-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0 0 20px 0;
  text-align: center;
}

.wheel-controls {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.wheel-row {
  display: flex;
  gap: 20px;
}

.wheel-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.wheel-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  text-align: center;
}

.slider-container {
  display: flex;
  align-items: center;
  gap: 12px;
}

.speed-slider {
  flex: 1;
  height: 8px;
  border-radius: 4px;
  background: var(--border-color);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.speed-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--primary-color);
  cursor: pointer;
  box-shadow: var(--shadow-light);
}

.speed-slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--primary-color);
  cursor: pointer;
  border: none;
  box-shadow: var(--shadow-light);
}

.speed-slider:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.speed-value {
  min-width: 40px;
  font-size: 14px;
  font-weight: 700;
  color: var(--text-primary);
  text-align: center;
}

.preset-buttons {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.preset-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 16px;
  font-size: 14px;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.preset-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.wheel-visualization {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.car-body {
  position: relative;
  width: 200px;
  height: 120px;
  background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-medium);
}

.wheel {
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.9);
  border: 3px solid var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  box-shadow: var(--shadow-light);
}

.wheel.active {
  background: var(--success-color);
  border-color: var(--success-color);
  animation: pulse 1s infinite;
}

.wheel-lf {
  top: -20px;
  left: -20px;
}

.wheel-rf {
  top: -20px;
  right: -20px;
}

.wheel-lr {
  bottom: -20px;
  left: -20px;
}

.wheel-rr {
  bottom: -20px;
  right: -20px;
}

.wheel-speed {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-primary);
}

.control-info {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
}

.info-label {
  font-size: 14px;
  color: var(--text-secondary);
}

.info-value {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

/* 移动端适配 */
@media (max-width: 768px) {
  .mecanum-wheel-container {
    padding: 16px;
  }
  
  .title {
    font-size: 20px;
  }
  
  .wheel-row {
    flex-direction: column;
    gap: 16px;
  }
  
  .preset-buttons {
    grid-template-columns: 1fr;
  }
  
  .car-body {
    width: 160px;
    height: 100px;
  }
  
  .wheel {
    width: 32px;
    height: 32px;
  }
  
  .wheel-lf, .wheel-rf {
    top: -16px;
  }
  
  .wheel-lr, .wheel-rr {
    bottom: -16px;
  }
  
  .wheel-lf, .wheel-lr {
    left: -16px;
  }
  
  .wheel-rf, .wheel-rr {
    right: -16px;
  }
}

/* 横屏适配 */
@media screen and (orientation: landscape) {
  .mecanum-wheel-container {
    padding: 16px 24px;
  }
  
  .content {
    flex-direction: row;
    gap: 20px;
  }
  
  .landscape-left {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 20px;
  }
  
  .landscape-right {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 20px;
  }
  
  .wheel-row {
    flex-direction: row;
  }
  
  .preset-buttons {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style> 